<?php
namespace Home\Controller;

use Home\Model\ApplicationFormModel;
use Home\Model\OfficeStationeryModel;

class CopyController extends CommonController{
	
  protected $table="application_form";
  protected $pageSize = 12;
  
  public function index(){
  	$res = D('OfficeStationery')->where(array('info_status'=>1))->select();
  	$this->assign('appType', $this->M->getType());
		$this->assign('appStatus', $this->M->getStatus());
		$this->assign('checkStatus', $this->M->getCheckStatus());
		$this->assign('departmentType', $this->M->getDepartmentType());
		$this->assign('monthType', $this->M->getMonthType());
		$this->assign('name',arr_to_map($res,name,true));
		$this->assign('unit',arr_to_map($res,unit,true));
		$this->assign('specification',arr_to_map($res,specification,true));
		$this->assign("userNames", D("ApplicationForm")->getUserNames());
  	$this->display();
  }

  public function getList(){
  	$name = $_SESSION['account']['userInfo']['user_id'];
	$arr = D('ApplicationForm')->field('id,send_copy')->select();
	$array= array();
	$id = array();
	foreach($arr as $k=>$value){
		$array[$k]['id'] = $value['id'];
		$array[$k]['send_copy'] = explode(',', $value['send_copy']);
	}
	foreach($array as $k=>$val){
		if(in_array($name, $val['send_copy'])){
			$id[]=$array[$k]['id'];
		}
	}
	
	if($id){
		$this->setQeuryCondition();
	    $count = (int)$this->M->where(array('id'=>array('IN',$id),'app_process'=>array('NEQ',-1)))->count();
	    $this->setQeuryCondition();
	    $list = $this->M->where(array('id'=>array('IN',$id),'app_process'=>array('NEQ',-1)))->page(I('get.p',0). ','. $this->pageSize)->order('id desc')->select();
	    
	    foreach($list as $k=>&$v){
	    	$id = $v['app_name'];
	        if($v['type']!=5){
	        	$re = D('user_info')->where(array('user_id'=>$id))->field('realname')->find();
				$v['app_name'] = $re['realname'];
	        }
	    }
		
		foreach($list as $k=>&$v){
			$re = $v['send_copy'];
			$res=explode(',',$re);
			$res1 = array();
			for($i=0;$i<count($res);$i++){
				$res1[] = D('user_info')->where(array('user_id'=>$res[$i]))->field('realname')->find();
			}
			
			
			$send_copy = "";
			$counts = count($res1);
			for($j = 0; $j < $counts; $j++){
			  $send_copy .= $res1[$j]['realname'].',';
			}
			$send_copy = substr($send_copy,0,strlen($send_copy)-1); 
			$v['send_copy'] = $send_copy;
		}
	    $result = array('list'=>$list, 'count'=>$count);
		$this->ajaxReturn($result);
	}
  }
  
 //设置查询
  public function setQeuryCondition(){

	if (I('get.type')) {
		$this->M->where(array("type"=> array('EQ', I('get.type'))));
	}
	
	if(I('get.app_status')){
		$this->M->where(array("app_status"=> array('EQ', I('get.app_status'))));
	}
	
	if(I('get.check_status')){
		$this->M->where(array("check_status"=> array('EQ', I('get.check_status'))));
	}
	
	if (I('get.start1') && I('get.start2')) {
		$start1 = I('get.start1');
		$start2 = I('get.start2');
		$d = date('Y-m-d',strtotime($start2)+86400);
		$this->M->where(array("app_time"=> array('between', "$start1,$d")));
	}
	
	if (I('get.end1') && I('get.end2')) {
		$end1 = I('get.end1');
		$end2 = I('get.end2');
		$d = date('Y-m-d',strtotime($end2)+86400);
		$this->M->where(array("exp_time"=> array('between', "$end1,$d")));
	}
		
    $this->M->where( array('info_status'=>array('EQ', 1)) );

  }












}